<?php
namespace app\controller;

use app\pub\Ants4PHP;
use app\pub\ErrJson;
use think\facade\Db;
use think\Request;

class Users {
    /**
     * 获取用户信息
     */
    public function getAllByUniacidAndPage(Request $request, $utype, $page, $limit, $search="",$manager=0,$grade_id=0){
        $uniacid = $request->header('uniacid');
        $where = [
            ['uniacid','=',$uniacid],
            ['utype','=',$utype]
        ];
        if($search != ""){
            $where[] = ['phone', 'like', $search];
        }
        if($manager != 0&&$manager != ""){
            $where[] = ['manager', '=', $manager];
        }
        if($grade_id != 0  && $grade_id != ""){
            $where[] = ['grade_id', '=', $grade_id];
        }
        // 获取用户信息
        $array = Db::name('ims_zst_marketing_users')
            ->where($where)
            ->order(['utime'=>'desc','id'=>'desc'])
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
//        echo Db::getLastSql();
        // 获取图片列表
        $total = $array->total();
        $list = $array->items();

        return ErrJson::success_json(array('total' => $total, 'list' => $list));
    }

    /**
     * 添加用户
     * data参数说明
     * @param id 如果有ID那么是修改，没有ID事新增
     * @param name 仓库名称    必填
     * @param phone 手机号码
     * @param group_id 分组的ID
     * @param storehouse_id 仓库或者门店的ID
     */
    public function save(Request $request){
        $uniacid = $request->header('uniacid');
        $params = $request->param();
        $params['uniacid'] = $uniacid;
        $params['utime'] = date("Y-m-d H:i:s");

//        return ErrJson::success_json($params);
        $totol = Db::name('ims_zst_marketing_users')->save($params);
        if($totol==1){
            return ErrJson::success_json('');
        }
        return ErrJson::error_json('提交失败');
    }

    /**
     * 删除用户
     * @param Request $request
     * @param $id 唯一ID
     */
    public function del(Request $request, $id){
        $uniacid = $request->header('uniacid');
        $totol = Db::table('ims_zst_marketing_users')
            ->where('id',$id)
            ->where('uniacid',$uniacid)
            ->delete();
        if($totol==1){
            return ErrJson::success_json('');
        }
        return ErrJson::error_json('删除失败');
    }

    /**
     * 获取详细信息
     * @param Request $request
     * @param $id 唯一ID
     */
    public function getInfoById(Request $request, $id){
        $uniacid = $request->header('uniacid');

        $data = Db::table('ims_zst_marketing_users')
            ->where('id', $id)
            ->where('uniacid',$uniacid)
            ->findOrEmpty();
        if(empty($data)){
            return ErrJson::error_json('该条数据无法查询');
        }
        return ErrJson::success_json($data);
    }

    /**
     * 获取用户分组列表
     * @param Request $request
     */
    public function getAllUserGrade(Request $request){
        $uniacid = $request->header('uniacid');

        $list = Db::table('ims_zst_marketing_users_grade')
            ->where('uniacid',$uniacid)
            ->select();

        return ErrJson::success_json($list);
    }

    /**
     * 添加分组
     * data参数说明
     * @param id null 唯一id
     * @param name 名称
     * @param m_price 划线价
     * @param price 优惠价
     * @param upgrade 免费升级要求
     * @param sort 级别
     * @param info 说明
     */
    public function saveUserGrade(Request $request){
        $uniacid = $request->header('uniacid');
        $params = $request->param();
        $params['uniacid'] = $uniacid;

        $totol = Db::name('ims_zst_marketing_users_grade')->save($params);
        if($totol==1){
            return ErrJson::success_json('');
        }
        return ErrJson::error_json('提交失败');
    }

    /**
     * 分页获取用户分组信息
     */
    public function getAllUserGradeByUniacidAndPage(Request $request, $page, $limit){
        $uniacid = $request->header('uniacid');
        $where = [
            ['uniacid','=',$uniacid]
        ];
        // 获取所有图片分类列表
        $array = Db::name('ims_zst_marketing_users_grade')
            ->where($where)
            ->order(['sort'=>'desc','id'=>'desc'])
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        // 获取图片列表
        $total = $array->total();
        $list = $array->items();

        return ErrJson::success_json(array('total' => $total, 'list' => $list));
    }

    /**
     * 获取会员等级详细信息
     */
    public function getUserGradeInfoByUniacidAndid(Request $request, $id){
        $uniacid = $request->header('uniacid');
        $data = Db::table('ims_zst_marketing_users_grade')
            ->where('id', $id)
            ->where('uniacid',$uniacid)
            ->findOrEmpty();
        if(empty($data)){
            return ErrJson::error_json('该条数据无法查询');
        }
        return ErrJson::success_json($data);
    }

}
